#include "gpio.h"
#include "ui_gpio.h"
#include "control.h"

gpio::gpio(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::gpio)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
}

gpio::~gpio()
{
    delete ui;
}

void gpio::on_pushButton_clicked()
{
    extern QSerialPort *serial;
    if(serial->isOpen()==true)
    {
    serial->write("@");
    serial->write(ui->GPIOX->currentText().toUtf8());
    serial->write("_");

    serial->write("PIN");
    serial->write( ui->PIN->currentText().toUtf8());
    serial->write("_");

    serial->write("SPEED");
    serial->write( ui->SPEED->currentText().toUtf8());
    serial->write("_");

    serial->write("MODE");
    if(strcmp(ui->MODE->currentText().toUtf8(), "IN")==0)
    {

       serial->write("0");
    }
    else if(strcmp(ui->MODE->currentText().toUtf8(), "OUT")==0)
    {
        if(strcmp(ui->OT->currentText().toUtf8(), "PP")==0)
        {

            serial->write("1");
        }
        else if((strcmp(ui->OT->currentText().toUtf8(), "OD")==0))
        {
            serial->write("2");

        }

    }
    else if(strcmp(ui->MODE->currentText().toUtf8(), "AF")==0)
    {
        if(strcmp(ui->OT->currentText().toUtf8(), "PP")==0)
        {
            serial->write("3");

        }
        else if((strcmp(ui->OT->currentText().toUtf8(), "OD")==0))
        {

            serial->write("4");
        }

    }
    else if(strcmp(ui->MODE->currentText().toUtf8(), "AIN")==0)
    {
        serial->write("5");
    }
    serial->write("_");


    serial->write("PULL");
     if(strcmp(ui->PUPDR->currentText().toUtf8(),"FLOAT")==0)
     {
           serial->write("0");

     }
     else if(strcmp( ui->PUPDR->currentText().toUtf8(),"DOWN")==0)
     {

         serial->write("2");

     }
     else if(strcmp( ui->PUPDR->currentText().toUtf8(),"UP")==0)
     {

         serial->write("1");

     }
    serial->write("_");

    serial->write("WRITE");
      if(strcmp( ui->WRITE->currentText().toUtf8(),"SET")==0)
      {
           serial->write("1");
      }
      else if(strcmp( ui->WRITE->currentText().toUtf8(),"RESET")==0)
      {
           serial->write("0");
      }

    serial->write("_");

    serial->write(" ");
    serial->write(".");
    }


    extern int tcp_state;
    extern QTcpSocket *qtcpsocket;
    if(tcp_state == 1)
    {
    qtcpsocket->write("@");
    qtcpsocket->write(ui->GPIOX->currentText().toUtf8());
    qtcpsocket->write("_");

    qtcpsocket->write("PIN");
    qtcpsocket->write( ui->PIN->currentText().toUtf8());
    qtcpsocket->write("_");

    qtcpsocket->write("SPEED");
    qtcpsocket->write( ui->SPEED->currentText().toUtf8());
    qtcpsocket->write("_");

    qtcpsocket->write("MODE");
    if(strcmp(ui->MODE->currentText().toUtf8(), "IN")==0)
    {

       qtcpsocket->write("0");
    }
    else if(strcmp(ui->MODE->currentText().toUtf8(), "OUT")==0)
    {
        if(strcmp(ui->OT->currentText().toUtf8(), "PP")==0)
        {

            qtcpsocket->write("1");
        }
        else if((strcmp(ui->OT->currentText().toUtf8(), "OD")==0))
        {
            qtcpsocket->write("2");

        }

    }
    else if(strcmp(ui->MODE->currentText().toUtf8(), "AF")==0)
    {
        if(strcmp(ui->OT->currentText().toUtf8(), "PP")==0)
        {
            qtcpsocket->write("3");
        }
        else if((strcmp(ui->OT->currentText().toUtf8(), "OD")==0))
        {

            qtcpsocket->write("4");
        }

    }
    else if(strcmp(ui->MODE->currentText().toUtf8(), "AIN")==0)
    {
        qtcpsocket->write("5");
    }
    qtcpsocket->write("_");


    qtcpsocket->write("PULL");
     if(strcmp(ui->PUPDR->currentText().toUtf8(),"FLOAT")==0)
     {
           qtcpsocket->write("0");

     }
     else if(strcmp( ui->PUPDR->currentText().toUtf8(),"DOWN")==0)
     {

         qtcpsocket->write("2");
     }
     else if(strcmp( ui->PUPDR->currentText().toUtf8(),"UP")==0)
     {

         qtcpsocket->write("1");

     }
    qtcpsocket->write("_");

    qtcpsocket->write("WRITE");
      if(strcmp( ui->WRITE->currentText().toUtf8(),"SET")==0)
      {
           qtcpsocket->write("1");
      }
      else if(strcmp( ui->WRITE->currentText().toUtf8(),"RESET")==0)
      {
           qtcpsocket->write("0");
      }

    qtcpsocket->write("_");
    qtcpsocket->write(" ");
    qtcpsocket->write(".");
    }
}





void gpio::on_pushButton_2_clicked()
{
    extern QSerialPort *serial;
    extern QTcpSocket *qtcpsocket;

    extern int tcp_state;
    int i;
       for(i=0;i<5;i++)
       {

           if(serial->isOpen()==true)
           {
               serial->write("@");
               serial->write("close");
               serial->write(" ");
               serial->write(".");
           }
       }

       if(tcp_state == 1)
       {
           qtcpsocket->write("@");
           qtcpsocket->write("close");
           qtcpsocket->write(" ");
           qtcpsocket->write(".");
       }
       this->close();
}
